### FOR FIGURE 5 IN THE MANUSCRIPT:

rm(list=ls())
library(foreign)
library(ggplot2) 
library(gridExtra)
library(knitr)
library(grid)
library(dplyr)
library(forcats)
#install.packages("extrafont")
library(extrafont)
#install.packages("performance")
library("performance")
library("see")
library("haven")
# Activate
library(DRDID)
library(did)

## Data to use 
setwd("~/Dropbox/Side projects/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Data")
setwd("~/Dropbox/EUI/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Estimates")

#allmap <- read_dta("data060121did.dta")
allmap <- read_dta("gambling_data.dta")

palette2 = c("red", "blue")

num2 <- att_gt(yname="num",
               tname="year",
               gname="tf",
               idname="school_id",
               data=allmap,
               bstrap=FALSE,
               cband=FALSE)

palette2 = c("red", "blue")

theme_results <- theme_bw(base_size = 10) +
  theme(text=element_text(family="Times",size = 16),
        axis.text.x = element_text(size = 15, color = "black"),
        axis.text.y = element_text(size = 15, color = "black"),
        axis.title.y = element_text(size = 20, color = "black"),
        axis.title.x = element_text(size = 20, color = "black"),
        legend.position = "bottom",
        legend.key = element_rect(fill = NA),
        legend.text = element_text(size = 20),
        legend.title = element_text(size = 20),
        strip.text.y.right = element_text(angle = 0),
        strip.background = element_rect(fill = "white"),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        legend.background=element_blank(),
        strip.text.x = element_text(size=20))

es2 <- aggte(num2, type = "dynamic")
santannaetal2 <- ggdid(es2) +
  scale_colour_manual(name = "",values = palette2, labels = c("Before BH\nopening", "After")) +
  geom_hline(yintercept = 0, 
             linetype = 2, color = "black") +
  ylab("New betting houses at less than 500m. on the\nno. of students sitting for the exam") +
  xlab("Time periods") +
  ylim(c(-20,35)) +
  theme_results +
  ggtitle("Using the Callaway Sant'Anna Estimator") +
  theme(panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        plot.title = element_text(hjust = 0.5))

es2

## Placebo: number of students:
setwd("~/Dropbox/Side projects/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Estimates")

all2 <- read_dta("num2.dta")

all2$id2 <- factor(all2$inc,
                   levels = c(0,1,2),
                   labels = c("Below average","All", "Above average"))

all2$y2 <- factor(all2$charter,
                  levels = c(0,1,2),
                  labels = c("Charter\nschools",
                             "Public\nschools",
                             "Overall\naverage"))

all2$year <- factor(all2$Year,
                    levels = c(1,2),
                    labels = c("Opening year t0",
                               "First full year t+1"))

palette2 = c("black", "blue", "red")

theme_results <- theme_bw(base_size = 15) +
  theme(text=element_text(family="Times",
                          size = 16),
        axis.text.x = element_text(size = 15, color = "black"),
        axis.text.y = element_text(size = 15, color = "black",angle=0),
        axis.title.y = element_text(size = 15, color = "black"),
        axis.title.x = element_text(size = 15, color = "black"),
        legend.position = "top",
        legend.key.size = unit(1.2, "cm"),
        legend.text = element_text(size = 15),
        legend.title = element_text(size = 15),
        panel.grid.major.y = element_blank(),
        panel.grid.minor.y = element_blank(),
        strip.text.y.right = element_text(angle = 0),
        strip.text.x = element_text(size=15)
  )

plotall2 <- all2 %>%
  ggplot(aes(x = y2, y = coef, colour = id2)) +   
  geom_point(size = 2 ,position = position_dodge(.5)) +
  geom_errorbar(aes(ymax = ci_upper95, ymin = ci_lower95), width = 0,  size = .7, position = position_dodge(width = .5)) +
  geom_errorbar(aes(ymax = ci_upper90, ymin = ci_lower90), width = 0, size = 1.6, position = position_dodge(width = .5)) +
  scale_color_manual(name = "Neighborhood\nby income levels:",values = palette2)+
  geom_hline(yintercept = 0, 
             linetype = 2, color = "black") + 
  xlab("") +
  ylab("New betting houses at less than 500m. on the\nno. of students sitting for the exam") +
  facet_wrap(year~., scales="free_x",nrow=1)+
  ggtitle("Using standard TWFE models") +
  theme_results + 
  theme(strip.background =element_rect(fill="white"),
        axis.title.y = element_text(size = 20, color = "black"),
        plot.title = element_text(hjust = 0.5),
        legend.position = "bottom")
plotall2

figure5 <- arrangeGrob(plotall2, santannaetal2, ncol=2)
#ggsave(file="placebos_120321.png", plot=g2, width = 30, height = 20, units = "cm") #saves g

